package vben.core.framework.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//springboot 整合 SecurityConfig后, 前端访问后端跨域的坑
//https://blog.csdn.net/qq_41463655/article/details/108038756
@Configuration
public class CorsConfig implements WebMvcConfigurer {

//    @Bean
//    public CorsFilter corsFilter() {
//        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
//        final CorsConfiguration corsConfiguration = new CorsConfiguration();
//        corsConfiguration.setAllowCredentials(true); /*是否允许请求带有验证信息*/
//        corsConfiguration.addAllowedOrigin("*");/*允许访问的客户端域名*/
//        corsConfiguration.addAllowedHeader("*");/*允许服务端访问的客户端请求头*/
//        corsConfiguration.addAllowedMethod("*"); /*允许访问的方法名,GET POST等*/
//        corsConfiguration.addExposedHeader("Authorization");
//        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
//        return new CorsFilter(urlBasedCorsConfigurationSource);
//    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // 使用模式匹配替代
                .allowCredentials(true)
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}

//@Configuration
//public class CorsConfig {
//    @Bean
//    public WebMvcConfigurer corsConfigurer() {
//        return new WebMvcConfigurer() {
//            @Override
//            public void addCorsMappings(CorsRegistry registry) {
//                registry.addMapping("/**")
////                        .allowedHeaders("Authorization")
//                        .allowedOrigins("*")
//                        .allowCredentials(true)
//                        .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
//                        .maxAge(3600);
//            }
//        };
//    }
//}